<?php

namespace App\Modules\Apicrzs\Services\Pay;

use App\Modules\Admin\Entities\Water\WaterOrderModel;

class PayNotifyServices
{

    protected static $instance;
    protected $type_attach;
    public function __construct($type_attach)
    {
        $this->type_attach =$type_attach;
    }
    /**
     * 实例化
     * @param array $config
     * @return static
     */
    public static function instance(array $config = [])
    {
        if (is_null(self::$instance)) {
            self::$instance = new static($config);
        }
        return self::$instance;
    }
    /**
     * 订单支付成功之后
     * @param string|null $order_id 订单id
     * @param string|null $trade_no
     * @param string $payType
     */
    public function handleNotify($data, $payType = 'weixin')
    {

        $order_sn = $data['out_trade_no'];
        $trade_no = $data['transaction_id'];
        $WaterOrderSuccessServices = new WaterOrderSuccessServices();
        if ($this->type_attach =='water_consume'){
            // 消费 打水
            $orderInfo = WaterOrderModel::getInstance()->getWaterOrderByOrderSn($order_sn);
            $order_type = $orderInfo['type'];
            if ($order_type=='consume') {
                xdebug($orderInfo, '打水支付-回调-api', '', 'pay/notify/' . $payType);
                return $WaterOrderSuccessServices->paySuccessConsume($orderInfo, $payType, ['trade_no' => $trade_no, 'desc' => $data['desc'] ?? []]);
            }else{
                // 套餐充值
                xdebug($orderInfo,'套餐充值-回调-api','','pay/notify/'.$payType);
                return $WaterOrderSuccessServices->paySuccessRecharge($orderInfo, $payType, ['trade_no' => $trade_no,'desc'=>$data['desc']??[]]);
            }
        }elseif ($this->type_attach =='water_recharge'){
            $orderInfo = WaterOrderModel::getInstance()->getWaterOrderByOrderSn($order_sn);

            // 套餐充值
            xdebug($orderInfo,'套餐充值-回调-api','','pay/notify/'.$payType);
            return $WaterOrderSuccessServices->paySuccessRecharge($orderInfo, $payType, ['trade_no' => $trade_no,'desc'=>$data['desc']??[]]);
        }elseif ($this->type_attach='mall_goods_order'){
            // 商城订单
            $orderInfo=[];
            $WaterOrderSuccessServices = new GoodsOrderSuccessServices();
            xdebug($orderInfo,'商城订单-回调-api','','pay/notify/mall/'.$payType);
            return $WaterOrderSuccessServices->paySuccessGoodsOrder($orderInfo, ['trade_no' => $trade_no]);
        }
        return false;
    }
}
